package cn.com.self.apis.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.UAccountInfo;
import cn.com.self.apis.member.entity.UMember;
import cn.com.self.apis.member.entity.UMemberInfo;
import cn.com.self.apis.member.entity.UMemberInfoRelation;
import cn.com.self.apis.member.service.IAccountService;
import cn.com.self.apis.member.service.IMemberInfoRelationService;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.member.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "登陆-注册")
public class LoginAndRegisterController {
    @Autowired
    private IMemberInfoService memberInfoService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IMemberInfoRelationService memberRelationService;

    @Autowired
    @Qualifier("consumerTokenServices")
    ConsumerTokenServices consumerTokenServices;


    /**会员注册
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/memberInfo/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "会员注册")
    @Transactional
    public ResponseEntity create(@RequestBody @Valid UMember entity, HttpServletRequest request){
        UMember uMember = memberService.queryMemberByPhone(entity.getPhone());
        if(uMember != null){
            return ResponseEntity.status(HttpStatus.FAILED_DEPENDENCY).body(new ResultModel<>("该手机号已注册！"));
        }
        //写入账号表
        UMember member = new UMember();
        member.setPassword(entity.getPassword());
        member.setPhone(entity.getPhone());
        member.setCreateTime(new Date());
        member.setUpdateTime(new Date());
        UMember memberCreate = memberService.create(member);
        //写入会员详细信息表
        UMemberInfo uMemberInfo = new UMemberInfo();
        uMemberInfo.setMemberId(memberCreate.getId());
        uMemberInfo.setTelephone(entity.getPhone());
        uMemberInfo.setCreateDate(new Date());
        uMemberInfo.setUpdateDate(new Date());
        uMemberInfo.setShowNum(new BigDecimal(0));
        uMemberInfo.setGrading("5");

        UMemberInfo create = memberInfoService.create(uMemberInfo);
        //初始化账号表
        UAccountInfo accountInfo = new UAccountInfo();
        accountInfo.setMemberId(create.getId());
        accountInfo.setCreateTime(new Date());
        accountInfo.setUpdateTime(new Date());
        accountService.create(accountInfo);
        //初始化擅长领域关联表
        //UMemberInfoRelation memberInfoRelation = new UMemberInfoRelation();
        //memberInfoRelation.setMemberInfoId(create.getId());
        //memberRelationService.create(memberInfoRelation);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    @RequestMapping(method = RequestMethod.POST, value = "/oauth/userLogout")
    @ResponseBody
    @ApiOperation(value = "注销登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "access_token", value = "用户登录凭证", required = true, dataType = "String")
    })
    public ResponseEntity revokeToken(String access_token) {
        if (consumerTokenServices.revokeToken(access_token)){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>("注销成功"));
        }else{
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>("注销失败"));
        }
    }


}

